<template>
  <view class="goodsList">
    <view class="goods-item">
      <!-- 商品左侧-->
      <view class="goods-left">
        <radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="changeRadio()"></radio>
        <image :src="goods.goods_small_logo || defaultPic"></image>
      </view>
      <!-- 商品右侧-->
      <view class="goods-right">
        <view class="goods-title">
          {{goods.goods_name}}
        </view>
          <view class="price">
            <view class="goods-price">
              ￥{{goods.goods_price | tofixed}}
            </view>
             <!-- 绑定改变的事件，不是点击，触发修改数量 -->
            <uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="changeNum"></uni-number-box>
          </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    // 定义 props 属性，用来接收外界传递到当前组件的数据
    props: {
      // 商品的信息对象
      goods: {
        type: Object,
        defaul: {},
      },
      // 控制购物车选中按钮是否显示,默认不显示
      showRadio: {
        type: Boolean,
        defaul: false,
      },
      //控制数量选择器的显示和隐藏,默认不显示
      showNum:{
        type:Boolean,
        default:false
      }
    },
    filters: {
      // 把数字处理为带两位小数点的数字
      tofixed(num) {
        return Number(num).toFixed(2)
      }
    },
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods:{
      changeRadio() {
        this.$emit('radioChange',{
          goods_id :this.goods.goods_id,
          goods_state: !this.goods.goods_state
        })
      },
      changeNum(val) {
        console.log(val);
        this.$emit('numChange',{
          goods_id :this.goods.goods_id,
          goods_count : val - 0
        })
      }
    }
  }
</script>

<style lang="scss">
  .goodsList {
    .goods-item {
      display: flex;
      padding: 10px;
      border-bottom: 1px solid #f0f0f0;
      .goods-left {
        margin-right: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        image {
          width: 100px;
          height: 100px;
        }
      }
      .goods-right {
        display: flex;
        flex: 1;
        flex-direction: column;
        justify-content: space-between;
        .goods-title {
          font-size: 13px;
        }
        .price{
          display: flex;
          justify-content: space-between;
          align-items: center;
          .goods-price {
            font-size: 17px;
            color: #c00000;
          }
        }
      }
    }
  }
</style>
